/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets.gp.medication;

import be.ugent.medirem.logic.MeasurementRemote;
import be.ugent.medirem.logic.MedicationRemote;
import be.ugent.medirem.persistence.entity.details.MeasurementTypeDetails;
import be.ugent.medirem.persistence.entity.details.UnitDetails;
import be.ugent.medirem.persistence.entity.values.MedicationValues;
import be.ugent.medirem.persistence.exception.EntityNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Simon
 */
public class AddMedicationServlet extends HttpServlet {
    @EJB
    private MeasurementRemote measurementBean;
    @EJB
    private MedicationRemote medicationBean;

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        List<UnitDetails> list = medicationBean.getAllUnits();
        List<MeasurementTypeDetails> types = measurementBean.getAllMeasurementTypes();

        request.setAttribute("units", list);
        request.setAttribute("types", types);
        request.getRequestDispatcher("/gp/medication/add.jsp").forward(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        MedicationValues m = (MedicationValues) request.getAttribute("medication");

        boolean validate = true;
        try {
            medicationBean.createMedication(m);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(AddMedicationServlet.class.getName()).log(Level.SEVERE, null, ex);
            validate = false;
        } catch (EntityNotFoundException ex) {
            Logger.getLogger(AddMedicationServlet.class.getName()).log(Level.SEVERE, null, ex);
            validate = false;
        } catch (Exception e) {
            validate = false;
        }

        if (validate) {
            request.setAttribute("flash", "Medication " + m.getName() + " added");
            response.sendRedirect(request.getContextPath()+"/gp/medication/list");
        }
        else {
            request.setAttribute("flash", "Could not create medication");
            doGet(request, response);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
